import EventEmitter from 'events';

export class TextModel extends EventEmitter{
  private _spacers: string = '';

  constructor () {
    super();
  }

  _emitTextChangeEvent () : void {
    this.emit('text_change');
  }

  getSpacers () : string {
    return this._spacers;
  }

  getSpacersByIndex (startIndex: number, endIndex: number) : string {
    return this._spacers.slice(startIndex, endIndex);
  }

  insert (spacerIndex: number, spacers: string) : void {
    const oldSpacers = this._spacers;
    this._spacers = oldSpacers.slice(0, spacerIndex) + spacers + oldSpacers.slice(spacerIndex);
    this._emitTextChangeEvent();
  }

  remove (startIndex: number, endIndex: number) : void {
    const oldSpacers = this._spacers;
    if (startIndex > endIndex) {
      [startIndex, endIndex] = [endIndex, startIndex];
    }
    this._spacers = oldSpacers.slice(0, startIndex) + oldSpacers.slice(endIndex);
    this._emitTextChangeEvent();
  }
}

export default TextModel;